import pandas as pd

from graph.JsonWR import read_json_to_groups
from utils.write import write_pkl


def save_graph_data(groups, t):
    columns = ["group", "count", "mean", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "min", "max"]
    df = pd.DataFrame(columns=columns)

    for key in groups:
        # print(groups.get(key))
        dataDict = {}
        value = groups.get(key)
        dataDict["group"] = "%s:%s" % (value.__getattribute__("intervalLeft"), value.__getattribute__("intervalRight"))
        dataDict["count"] = value.get_count_sum()
        dataDict["mean"] = value.get_global_mean()
        for i in range(0, 11):
            dataDict[str(i)] = value.get_percentile(i)
        # print(dataDict, type(dataDict))
        dataDict["min"] = value.get_min()
        dataDict["max"] = value.get_max()
        row = pd.DataFrame(data=dataDict, index=[0])
        df = df.append(row)

    # print(df.info(), type(df))
    write_pkl("data/graph_%s_%s.pkl" % ("single", t), df)
    df.to_csv("data/graph_%s_%s.csv" % ("single", t))


if __name__ == '__main__':
    # 绘图数据准备
    groupsT = read_json_to_groups("../cluster/data/group_%s_%s.json" % ("single", "T"))
    save_graph_data(groupsT, "T")
    groupsF = read_json_to_groups("../cluster/data/group_%s_%s.json" % ("single", "F"))
    save_graph_data(groupsF, "F")
    groupsTF = read_json_to_groups("../cluster/data/group_%s_%s.json" % ("single", "TF"))
    save_graph_data(groupsTF, "TF")
